package com.ydm.myapp001.application;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.ydm.myapp001.utils.AppLogger;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yangdongming on 2017/12/14.
 */

public class MyApplication extends Application {
    private static final String TAG = "YDMLog_MyApplication";

    private float screenDensity;//屏幕密度

    public float getScreenDensity() {
        return screenDensity;
    }
    public void setScreenDensity(float screenDensity) {
        this.screenDensity = screenDensity;
    }


    private String[] tagArr = {"orderOpen", "getProductInfo", "orderScan", "memberLogin", "paymentCreate", "orderPayment"};//单一请求接口tag罗列

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: " + "我是Application类");
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);


        //test();
        printAndroiodScreenProperty(this);


        //test();

        //
        isShouldRequestInterface("confi");
        isShouldRequestInterface("orderOpen");
        isShouldRequestInterface("memberLogin");
        isShouldRequestInterface("memberLogin1");
    }


    public void test() {
        //1.存储卡路径
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
        Log.d(TAG, "path: " + path); //打印结果 -- path: /storage/emulated/0/Download
        //2.文件
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "myApp.apk");
        Log.d(TAG, "path: " + file.getPath()); //打印结果 -- path: /storage/emulated/0/Download/myApp.apk


        //3.Byte[]与String转换
        try {
            byte[] gbk_Bytes = "中国1988人民万1119岁中国19881中国9人11岁".getBytes("GB2312");
            Log.d(TAG, "gbk_Bytes长度 " + gbk_Bytes.length);
            byte[] gbk_Bytes_32 = subBytes(gbk_Bytes, 0, 32);
            String gbk_String = new String(gbk_Bytes_32, "GB2312");
            if (gbk_String.endsWith("�")) { //说明转化失败
                byte[] gbk_Bytes_31 = subBytes(gbk_Bytes, 0, 31);
                String gbk_String_31 = new String(gbk_Bytes_31, "GB2312");
                Log.d(TAG, "gbk_String_31: " + gbk_String_31);
            } else {
                Log.d(TAG, "gbk_String: " + gbk_String);
            }




            String s000001 = "清山";
            Log.d(TAG, "s: " + s000001);
            byte[] b = s000001.getBytes("utf-8");//编码
            String sa000001 = new String(b, "gb2312");//解码:用什么字符集编码就用什么字符集解码
            Log.d(TAG, "sa000001: " + sa000001);

        }
        catch ( UnsupportedEncodingException e) {
            Log.d(TAG, "不支持的字符集");
        }

    }


    /**
     * 从一个byte[]数组中截取一部分
     * @param src
     * @param begin
     * @param count
     * @return
     */
    public byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
        return bs;
    }


    public void  printAndroiodScreenProperty(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)


        Log.d(TAG, "屏幕宽度（像素px）：" + width);
        Log.d(TAG, "屏幕高度（像素px）：" + height);
        Log.d(TAG, "屏幕密度（0.75 / 1.0 / 1.5）：" + density);
        Log.d(TAG, "屏幕密度dpi（120 / 160 / 240）：" + densityDpi);
        Log.d(TAG, "屏幕宽度（dp）：" + screenWidth);
        Log.d(TAG, "屏幕高度（dp）：" + screenHeight);
        screenDensity = density;
    }


    public void isShouldRequestInterface(String requestTag) {
        boolean isSingleReqInterface = false;
        if (Arrays.asList(tagArr).contains(requestTag)) {
            isSingleReqInterface = true;
        }

        AppLogger.d("MySelf", isSingleReqInterface ? "是" : "否");
    }
}
